package com.bjut.vendormgrsys.util;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalField;
import java.util.Calendar;
import java.util.Locale;

public class DateUtils {

    public enum Deliver {
        DAY_OF_THREE("3D"),
        WEEK_OF_ONE("1W"),
        MONTH_OF_ONE("1M"),
        MONTH_OF_SIX("6M");

        private String key;

        Deliver(String key) {
            this.key = key;
        }

        public String getKey() {
            return key;
        }

        public void setKey(String key) {
            this.key = key;
        }
    }

    public enum Pattern {
        PATTERN_DEFAULT("yyyy-MM-dd HH:mm:ss"),
        PATTERN_ONLY_DATE("yyyy-MM-dd"),
        PATTERN_ONLY_TIME("HH:mm:ss");
        private String patter;

        Pattern(String pattern) {
            this.patter = pattern;
        }

        public String getPatter() {
            return patter;
        }

        public void setPatter(String patter) {
            this.patter = patter;
        }
    }

    public static String formatDate(long date, Pattern pattern) {
        LocalDateTime localDateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(date), ZoneId.systemDefault());
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern(pattern.getPatter());
        return localDateTime.format(dtf);
    }

    public static long parseDate(String date, Pattern pattern) {
        LocalDateTime localDateTime =
                LocalDateTime.parse(date, DateTimeFormatter.ofPattern(pattern.getPatter()));
        return Timestamp.valueOf(localDateTime).getTime();
    }

    public static long addDay(long millis, int amount) {
        return add(millis, Calendar.DAY_OF_MONTH, amount);
    }

    public static long addWeek(long millis, int amount) {
        return add(millis, Calendar.WEEK_OF_MONTH, amount);
    }

    public static long addMonth(long millis, int amount) {
        return add(millis, Calendar.MONTH, amount);
    }

    private static long add(long millis, int field, int amount) {
        Calendar calendar = Calendar.getInstance(Locale.CHINA);
        calendar.setTimeInMillis(millis);
        calendar.add(field, amount);
        return calendar.getTimeInMillis();
    }

    public static long now() {
        return LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static long day3() {
        LocalDateTime localDateTime = LocalDateTime.now().plusDays(-3);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static long week1() {
        LocalDateTime localDateTime = LocalDateTime.now().plusWeeks(-1);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static long month1() {
        LocalDateTime localDateTime = LocalDateTime.now().plusMonths(-1);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static long month6() {
        LocalDateTime localDateTime = LocalDateTime.now().plusMonths(-6);
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }
}
